package com.leetcode.hash.demo4;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Zan
 * @Create 2024/5/22 10:40
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/two-sum/description/  两数之和
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Solution {
    public int[] twoSum(int[] nums, int target) {
        if (nums.length == 0 && nums == null) {
            return null;
        }
        int[] arr = new int[2];
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp)) {
                arr[0] = map.get(temp); // 直接获取下标
                arr[1] = i;
            }
            map.put(nums[i], i); // 思路，调换顺序传参，value是下标
        }
        return arr;
    }
}